using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using TuringMachine.StateAutomat;

namespace TuringMachine.CompilerModule
{
    class Parser
    {
        private string _currentCommand;
        public string _startStateName;
        public string _inputChar;
        public string _nextStateName;
        public string _tapeAction;

        public string CurrentCommand
        {
            get { return _currentCommand; }
            set { _currentCommand = value; }
        }
        public Parser()
        {
            this.CurrentCommand = string.Empty;
        }

        public void Parse(string command)
        {
            string[] elementsArray = null;
            this.CurrentCommand = command;
            if (!IsLegal())
            {
                throw new Exception("Illegal command " + this.CurrentCommand);
            }
            elementsArray = this.CurrentCommand.Split(',');
            _startStateName = elementsArray[0];
            _inputChar = elementsArray[1];
            _nextStateName = elementsArray[2];
            _tapeAction = elementsArray[3];
        }
        public State StartState()
        {
            StateType type = StateType.UnknownState;
            switch (_startStateName)
            {
                case("S"):
                    type = StateType.StartState;
                    break;
                case("H"):
                    type = StateType.HaltState;
                    break;
                case("Y"):
                    type = StateType.YesState;
                    break;
                case("N"):
                    type = StateType.NoState;
                    break;
                default:
                    type = StateType.MiddleState;
                    break;
            }

            return (new State(_startStateName, type));
        }

        public char InputChar()
        {
            return ((_inputChar.ToCharArray())[0]);
        }

        public State NextState()
        {
            StateType type = StateType.UnknownState;
            switch (_nextStateName)
            {
                case ("S"):
                    type = StateType.StartState;
                    break;
                case ("H"):
                    type = StateType.HaltState;
                    break;
                case ("Y"):
                    type = StateType.YesState;
                    break;
                case ("N"):
                    type = StateType.NoState;
                    break;
                default:
                    type = StateType.MiddleState;
                    break;
            }

            return (new State(_nextStateName, type));
        }

        public char TapeAction()
        {
            return ((_tapeAction.ToCharArray())[0]);
        }

        private bool IsLegal()
        {
            string pattern = "[0-9a-zA-Z]{1,4},{1}[0-9a-zA-Z_#]{1},{1}[0-9a-zA-Z_]{1,4},{1}[0-9a-zA-Z_<>]{1}";
            return Regex.Match(this.CurrentCommand,pattern,RegexOptions.None).Success;
        }
    }
}
